package cn.com.chengmandian.core.lot.utils;

public class BytesHexReverseUtil {
    public static void main(String[] args) {
        byte[] bytes = new byte[]{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        System.out.println(sixteenBytesTo4HexReverse(bytes));
//        转换之后 0300

        byte[] bytes1 = hexTo16BitsReverse("0300", 12);
        for (byte b : bytes1) {

            System.out.println(b);
        }
    }

    public static String sixteenBytesTo4HexReverse(byte[] input) {
        byte[] sixteenBytes = new byte[16];
//        一般是11位电池
        int inputLength = input.length;
        //前面的数组复制过来
        System.arraycopy(input, 0, sixteenBytes, 0, inputLength);
        //若不满 16 位,则后面补 0, 如 11 位要在后面补 5 个 0, 8 位则后面 8 位补零
        for (int i = inputLength; i < 16; i++) {
            sixteenBytes[i] = 0;
        }
        byte[] reverseBytes = reverseArray(sixteenBytes);
        String one = Integer.toHexString(reverseBytes[0] * 8 + reverseBytes[1] * 4 + reverseBytes[2] * 2 + reverseBytes[3]);
        String two = Integer.toHexString(reverseBytes[4] * 8 + reverseBytes[5] * 4 + reverseBytes[6] * 2 + reverseBytes[7]);
        String three = Integer.toHexString(reverseBytes[8] * 8 + reverseBytes[9] * 4 + reverseBytes[10] * 2 + reverseBytes[11]);
        String four = Integer.toHexString(reverseBytes[12] * 8 + reverseBytes[13] * 4 + reverseBytes[14] * 2 + reverseBytes[15]);

//        转为小端, 如 001b 转为 1b00
        return three + four + one + two;
    }

    /**
     * 反转数组
     */
    public static byte[] reverseArray(byte[] array) {
        byte[] newArray = new byte[array.length];
        for (int i = 0; i < newArray.length; i++) {
            newArray[i] = array[array.length - i - 1];
        }
        return newArray;
    }

    /**
     * 2字节转成 16 位数组,再反转成最终输出
     * 如 1b00,先转为 001b,
     * 再转了之后是[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1]
     * 再反转成[[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     * 0300 转换成 [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     * 030000 转换成 [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     */
    public static byte[] hexTo16BitsReverse(String hexStr, int byteLength) {
        if (hexStr.length() < 4 || (hexStr.length() % 2) != 0) {
//            若长度小于 4,或长度不是偶数,则数值有问题
            return new byte[byteLength];
        }
//        如 1b00,先转为 001b
        StringBuilder newHexStr = new StringBuilder();
        int newHexStrLength = hexStr.length() / 2;
        for (int i = newHexStrLength; i > 0; i--) {
            int startIndex = (i - 1) * 2;
            newHexStr.append(hexStr, startIndex, startIndex + 2);
        }

//        十六进制转 10 进制
        int sum = Integer.parseInt(newHexStr.toString(), 16);
//        转成字节数组
        byte[] bmsBytes = byte2ToBitReverse(sum, byteLength);
//        再反转
        return reverseArray(bmsBytes);
    }

    /**
     * 第一位与最后一位颠倒过来
     */
    public static byte[] byte2ToBitReverse(int b, int bytesLength) {
        byte[] bits = new byte[bytesLength];
        for (int i = 0; i < bytesLength; i++) {
            if (i == bytesLength - 1) {
                bits[i] = (byte) (b & 0x1);
            }
            bits[i] = (byte) ((b >> bytesLength - i - 1) & 0x1);
        }

        return bits;
    }

}
